package server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import obj.MyObject;

public class StartServer {
	public static void main(String[] args) {
		int sessionNum = 0;
		ServerSocket serverSocket = null;
		try {
			System.out.println("Starting server!");
			serverSocket = new ServerSocket(4444);
			Socket socket = serverSocket.accept();
			ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
			ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());

			Object obj = null;
			System.err.println("Server was created!");
			while ((obj = inputStream.readObject()) != null) {
				if (obj instanceof MyObject) {
					MyObject myObject = (MyObject) obj;
					String echoStr = "Server echo : " + myObject.getStr() + " _ Session: " + sessionNum++;
					myObject.setStr(echoStr);
					outputStream.writeObject(myObject);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
